unit Corretor_CancPedido;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Gauges, ExtCtrls, db, dbtables;

type
  TfrmCorretor_PedCanc = class(TForm)
    Bevel1: TBevel;
    pb: TGauge;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Altera_Status(tq1: TQuery);
    { Public declarations }
  end;

var
  frmCorretor_PedCanc: TfrmCorretor_PedCanc;

implementation

uses clsBackup_Pedido, clsPedidosVenda, clsItemPedVenda, clsBackupVenda, Dm;

{$R *.DFM}

procedure TfrmCorretor_PedCanc.Altera_Status;
var
  not1, not2: string;
  tq: TQuery;
  bkpPed: TBackupPedido;
  backup: TBackupVenda;
  PedVenda: TPedidosVenda;
  ItPv: TItemPedVenda;
begin
  backup := TBackupVenda.Create;
  PedVenda := TPedidosVenda.Create;
  ItPv := TItemPedVenda.Create;
  bkpPed := TBackupPedido.Create;
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;

  backup.RecuperarNota(tq1.FieldByName('Bkp_Serie').AsString, tq1.FieldByName('Bkp_Numero').AsString, not1, not2);
  ItPv.Recuperar_porNota(not1, not2, tq1.FieldByName('Bkp_Serie').AsString, tq1.FieldByName('Ent_Codigo').AsString, tq);
  while not tq.eof do begin
    PedVenda.Recuperar(tq.FieldByName('Ped_Numero').AsString, tq.FieldByName('Ped_Numero').AsString);
    if (PedVenda.Ped_situacao <> 'K') and (PedVenda.Ped_situacao <> 'C') then
      PedVenda.Cancela_Pedido(bkpPed.RecuperarPedido2(tq.FieldByName('Ped_Numero').AsString), tq1.FieldByName('Notv_DataFat').AsDateTime);
    tq.Next;
  end;
  tq.Close;
  tq.Free;
  bkpPed.Free;
  backup.Free;
  PedVenda.Free;
  ItPv.Free;
end;

procedure TfrmCorretor_PedCanc.BitBtn1Click(Sender: TObject);
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.Add('Select distinct Bkp_Numero, Bkp_Serie, B.Ent_Codigo, Notv_datafat ' +
    'from NotaVenda N, Backup_Venda B where CANP_CODIGO is not null  ' +
    'and (B.Nota_Numero = N.Nota_Numero and B.Nota_Serie = N.Nota_Serie ' +
    'and B.Ent_Codigo = N.Ent_Codigo)');
  tq.Open;
  pb.MinValue := 0;
  pb.MaxValue := 0;
  pb.Progress := tq.RecordCount;
  while not tq.Eof do begin
    pb.Progress := pb.Progress + 1;
    Altera_Status(tq);
    tq.Next;
  end;
  ShowMessage('Pedidos corrigidos com sucesso');
  tq.Close;
  tq.Free;
end;

procedure TfrmCorretor_PedCanc.BitBtn3Click(Sender: TObject);
begin
  Close;
end;

end.
